const express=require('express')
const router=express.Router()

const multer=require('multer')
//图片过滤器
var storage=multer.diskStorage({
    destination:function(req,file,cb){
        //指定文件路径
        cb(null,'./static/img')
    },
    filename:function(req,file,cb){
        ////指定文件名
        console.log('-----',file)
        let ext=file.originalname.split('.')[1]
        //let name=file.originalname.split('.')[0]
        let name=new Date().getTime()+parseInt(Math.random()+999)
        cb(null,`${name}.${ext}`)
    }
})
var upload=multer({storage:storage})

//hehe图片的key值
router.post('/upload',upload.single('hehe'),(req,res)=>{
    console.log(req.file)
    let {size,mimetype,path}=req.file
    let types=['jpg','jpeg','png','gif']
    let tmtype=mimetype.split('/')[1]
    if(size>500000){
        return res.send({err:-1,msg:'尺寸过大'})
    }else if(types.indexOf(tmtype)==-1){
        return res.send({err:-2,msg:'上传类型错误'})
    }else{
        res.send({err:0,msg:'上传ok',data:'/public/img/'+req.file.filename})
    }
    
})


module.exports=router